package com.granada.utils;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20190711.SmsClient;
import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 腾讯云短信平台
 */
@Slf4j
public class SMSUtil {

    public static String secretId = "AKIDvmdHftrq9CuNaX8jy1ogHcJn01YphLHx";

    public static String secretKey = "d1QbXaXhcMsjQyRQEmX4SfR3ZpwDOQNB";

    public static String appid = "1400505919"; //你创建的应用AppID

    public static String templated = "1284560"; //你的正文模板ID

    public static String updatePasswordId = "916421"; //修改密码的正文模板ID

    public static String commentId = "1272560"; //评价短信

    public static String DemandNotificationId = "1272571"; //需求通知短信

    public static String manufacturerId = "1272572"; //厂商发送给企业的正文模板ID

    public static String enterpriseId = "1272573"; //企业发送给厂商的正文模板ID

    public static String smsSign = "服至网络";  //你的签名模板中的内容,不是签名ID

    public static String url = "sms.tencentcloudapi.com";  //请求地址

    private static final Logger log = LoggerFactory.getLogger(SMSUtil.class);


//    public static void main(String [] args) {
//        SMSUtil smsUtil = new SMSUtil();
//        smsUtil.sendSms("+8618618419775","666666");
//    }

    /**
     * 发送短信
     */
    public void sendSms(String phoneNum,String vaildCode) {
        try{
                Credential cred = new Credential(secretId, secretKey);

                HttpProfile httpProfile = new HttpProfile();
                httpProfile.setEndpoint(url);

                ClientProfile clientProfile = new ClientProfile();
                clientProfile.setHttpProfile(httpProfile);
                SmsClient client = new SmsClient(cred, "", clientProfile);
                SendSmsRequest req = new SendSmsRequest();
                String[] phoneNumberSet1 = {phoneNum};
                req.setPhoneNumberSet(phoneNumberSet1);
                req.setTemplateID(templated);
                req.setSmsSdkAppid(appid);
                req.setSign(smsSign);
                String [] param = {vaildCode};
                req.setTemplateParamSet(param);

                SendSmsResponse resp = client.SendSms(req);

                System.out.println("腾讯云短信平台返回信息：：："+SendSmsResponse.toJsonString(resp));
            } catch (TencentCloudSDKException e) {
                System.out.println(e.toString());
            }
    }


    /**
     * 企业发送短信给厂商
     */
    public void enterpriseSendSms(String phoneNum,String ClientRealName,String manufacturerName) {
        try{
            Credential cred = new Credential(secretId, secretKey);

            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint(url);

            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            SmsClient client = new SmsClient(cred, "", clientProfile);
            SendSmsRequest req = new SendSmsRequest();
            String[] phoneNumberSet1 = {"+"+"86"+phoneNum};
            req.setPhoneNumberSet(phoneNumberSet1);
            req.setTemplateID(enterpriseId);
            req.setSmsSdkAppid(appid);
            req.setSign(smsSign);
            String [] param = {manufacturerName,ClientRealName};
            req.setTemplateParamSet(param);

            SendSmsResponse resp = client.SendSms(req);

            System.out.println("腾讯云短信平台返回信息：：："+SendSmsResponse.toJsonString(resp));
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
        }

    }

    /**
     * 厂商发短信给企业
     */
    public void manufacturerSendSms(String phoneNum,String ClientRealName,String manufacturerName) {
        try{
            Credential cred = new Credential(secretId, secretKey);

            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint(url);

            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            SmsClient client = new SmsClient(cred, "", clientProfile);
            SendSmsRequest req = new SendSmsRequest();
            String[] phoneNumberSet1 = {"+"+"86"+phoneNum};
            req.setPhoneNumberSet(phoneNumberSet1);
            req.setTemplateID(manufacturerId);
            req.setSmsSdkAppid(appid);
            req.setSign(smsSign);
            String [] param = {ClientRealName,manufacturerName};
            req.setTemplateParamSet(param);

            SendSmsResponse resp = client.SendSms(req);

            System.out.println("腾讯云短信平台返回信息：：："+SendSmsResponse.toJsonString(resp));
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
        }

    }


    /**
     * 评价短信
     */
    public void commentSendSms(String phoneNum,String ClientRealName,String manufacturerName) {
        try{
            Credential cred = new Credential(secretId, secretKey);

            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint(url);

            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            SmsClient client = new SmsClient(cred, "", clientProfile);
            SendSmsRequest req = new SendSmsRequest();
            String[] phoneNumberSet1 = {"+"+"86"+phoneNum};
            req.setPhoneNumberSet(phoneNumberSet1);
            req.setTemplateID(commentId);
            req.setSmsSdkAppid(appid);
            req.setSign(smsSign);
            String [] param = {manufacturerName,ClientRealName};
            req.setTemplateParamSet(param);

            SendSmsResponse resp = client.SendSms(req);

            System.out.println("腾讯云短信平台返回信息：：："+SendSmsResponse.toJsonString(resp));
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
        }
    }

    /**
     * 评价短信
     */
    public void demandNotificationSms(String phoneNum,String ClientRealName,String manufacturerName) {
        try{
            Credential cred = new Credential(secretId, secretKey);

            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint(url);

            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            SmsClient client = new SmsClient(cred, "", clientProfile);
            SendSmsRequest req = new SendSmsRequest();
            String[] phoneNumberSet1 = {"+"+"86"+phoneNum};
            req.setPhoneNumberSet(phoneNumberSet1);
            req.setTemplateID(DemandNotificationId);
            req.setSmsSdkAppid(appid);
            req.setSign(smsSign);
            String [] param = {manufacturerName,ClientRealName};
            req.setTemplateParamSet(param);

            SendSmsResponse resp = client.SendSms(req);

            System.out.println("腾讯云短信平台返回信息：：："+SendSmsResponse.toJsonString(resp));
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
        }
    }



    /**
     * 手机号验证
     * @author ：Wu.H  2021-04-13
     * @param  str
     * @return 验证通过返回true
     */
    public boolean isMobile(final String str) {
        Pattern p = null;
        Matcher m = null;
        boolean b = false;
        p = Pattern.compile("^[1][3,4,5,7,8][0-9]{9}$"); // 验证手机号
        m = p.matcher(str);
        b = m.matches();
        return b;
    }


//    public static void main(String [] args) {
//        try{
//
//                Credential cred = new Credential(secretId, "d1QbXaXhcMsjQyRQEmX4SfR3ZpwDOQNB");
//
//                HttpProfile httpProfile = new HttpProfile();
//                httpProfile.setEndpoint("sms.tencentcloudapi.com");
//
//                ClientProfile clientProfile = new ClientProfile();
//                clientProfile.setHttpProfile(httpProfile);
//
//                SmsClient client = new SmsClient(cred, "", clientProfile);
//
//                SendSmsRequest req = new SendSmsRequest();
//                String[] phoneNumberSet1 = {"+8618618419775"};
//                req.setPhoneNumberSet(phoneNumberSet1);
//
//                req.setTemplateID("916421");
//                req.setSmsSdkAppid("1400505919");
//                req.setSign("服至网络");
//                String [] param = {"666666"};
//                req.setTemplateParamSet(param);
//                SendSmsResponse resp = client.SendSms(req);
//
//                System.out.println(SendSmsResponse.toJsonString(resp));
//            } catch (TencentCloudSDKException e) {
//                System.out.println(e.toString());
//            }
//
//    }








}
